<template>
	<view class="login_index">
		<view class="login_index_box">
			<view class="login_index_logo">
				<image class="login_index_logo_icon" src="../../static/img/companyLogo.png" 
					style="width: 174rpx;height: 174rpx;"></image>
			</view>
			<view class="login_index_logo1">
				上海建工 基础集团
				<!-- <image class="login_index_logo_icon1" src="../../static/img/ companTitle.png" 
					style="width: 386rpx;height: 109rpx;"></image> -->
			</view>
			<view style="    text-align: center;
    margin-top: 35rpx;
    font-size: 30rpx;
	    color: #676363;
    margin: 35rpx 160rpx;
    font-family: 'HanYiZongYi';
    line-height: 43rpx;">
				此小程序仅为施工相关的人员进行操作使用，请仔细阅读协议
			</view>
		</view>
		
		<view class="login_index_btn"  style="display: flex;"  v-if="!value  && code=='1'">
			<button  class="phone-button"  @click="checkAgreement">授权使用</button>
			<navigator    open-type='exit' target='miniProgram'>
				<button style="    background: #e0dddd;
    color: #333333;">取消授权</button>
			</navigator>

		</view>
		
		<!-- <view class="login_index_btn" v-else-if="value && code == '2'" @click="getcode()">
			<button class="phone-button">登录</button>

		</view> -->
		<view class="login_index_btn" v-else-if=" code == '3'">
			<button class="phone-button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权使用</button>

		</view>
		<tn-toast ref="toast"></tn-toast>
		<view class="login_index_agree">
			<tn-radio-group >
				<tn-checkbox   @change="checkboxChange" v-model="value" :disabledLabel="true" shape="circle">您已认真阅读并同意
					<span style="color: #5E6C8C;" @click="redirectToPrivacyPage">《隐私条款》</span> 和 <span
						style="color: #5E6C8C;" @click="redirectToPrivacyPage1">《用户协议》</span></tn-checkbox>

			</tn-radio-group>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: false,
				code: '1',
				phone: '',
				UNIONID:''
				
			}
		},
		onLoad() {
			// this.fetchData();
			// this.postData();
			this.getcode();

		},
		onBackPress() {
			return true;
		},
		methods: {

	
			redirectToPrivacyPage() {
				// 跳转到名为 privacy 的页面
				uni.navigateTo({
					url: '/pages/privacy/privacy'
				});
			},
			redirectToPrivacyPage1() {
				// 跳转到名为 privacy 的页面
				uni.navigateTo({
					url: '/pages/privacyuser/privacyuser'
				});
			},
			checkboxChange() {
				// 点击圆圈图标时切换状态
				this.value = !this.value;
				if (this.value == true) {
					this.code = '3'
				} else {
					this.code = '1'
				}
				console.log(this.code, ' this.code')
			},
			getcode() {
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {

						this.sessionId = loginRes.code;
					
						try {
							const url = '/portal/r/w'; // 替换为你的 API 地址
							const params = {
								// 构造你的请求参数
								type: '1',
								cmd: 'com.apps.sfeg.srm.login',
								code: this.sessionId
							};

							// 发送 GET 请求
							const response = await this.$get(url, params);
							console.log(response,'response')
							this.UNIONID=response.data.UNIONID;
							uni.setStorageSync('UNIONID', response.data.UNIONID);
							uni.setStorageSync('OPEN_ID', response.data.OPENID);
							uni.setStorageSync('IS_LEADER', response.data.IS_LEADER);
							uni.setStorageSync('SID', response.data.SID);
							uni.setStorageSync('phoneNumber', response.data.MOBILE);
							if (response.result == 'error') {


								this.code = '1';

							} else if (response.result == 'ok' && response.data.IS_LEADER == '0') {
								console.log('我是注册过的劳务工人');

								
								uni.setStorageSync('IS_LEADER', response.data.IS_LEADER);
								uni.switchTab({
									url: '../../pages/index/index'
								})

							} else if (response.result == 'ok' && response.data.IS_LEADER == '1') {
								console.log('我是注册过的分包领导');


								uni.switchTab({
									url: '../../pages/leaderindex/leaderindex'
								})

							}
							// 处理请求成功的逻辑



						} catch (error) {
							// 处理请求失败的逻辑
							console.error('请求失败', error);
						}


					},
					fail: function(err) {}
				});
			},
			checkAgreement() {
				// uni.switchTab({
				// 	url: '/pages/index/index'
				// });
				if (!this.value) {
					// 如果未同意隐私条款，则显示提示信息
					this.$refs.toast.show({

						content: '请先同意隐私条款',
						icon: 'none', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});
				} else {
					// 如果已同意隐私条款，则执行手机号登录的逻辑

				}
			},
			async getPhoneNumber(e) {
				// uni.switchTab({
				// 	url: '/pages/my/my'
				// });


				const code = e.detail.code;
				console.log(code);
				var that = this;
				if (e.detail.errMsg === "getPhoneNumber:ok") {
					try {
						const url = '/portal/r/w'; // 替换为你的 API 地址
						const params = {
							// 构造你的请求参数

							cmd: 'com.apps.sfeg.srm.moblie',
							code: code
						};

						// 发送 GET 请求
						const response = await that.$get(url, params);
						console.log('请求成功11111', response);
						that.phone = response.data.MOBILE;
						uni.setStorageSync('phoneNumber', response.data.MOBILE);
						if (response.result == 'ok') {



							try {
								let url = '/portal/r/w'; // 替换为你的 API 地址
								let params = {
									// 构造你的请求参数
									type: '2',
									cmd: 'com.apps.sfeg.srm.login',
									mobile: that.phone,
									openId: uni.getStorageSync('OPEN_ID'),
									
									unionid:that.UNIONID
								};

								// 发送 GET 请求
								var response1 = await that.$get(url, params);
								console.log('请求成功手机登录', response1);
									uni.setStorageSync('SID', response1.data.SID);
								if (response1.result == 'error' && response1.data.IS_LEADER == '0') {
									uni.setStorageSync('IS_LEADER', response1.data.IS_LEADER);
									uni.redirectTo({
										url: '../../pages/regist/regist'
									});
								} else if (response1.result == 'ok' && response1.data.IS_LEADER == '0'){
									
									uni.setStorageSync('IS_LEADER', response1.data.IS_LEADER);
								uni.switchTab({
									url: '../../pages/index/index'
								})
								}
								else {

									uni.setStorageSync('SID', response1.data.SID);
									uni.setStorageSync('IS_LEADER', response1.data.IS_LEADER);
									uni.switchTab({
										url: '../../pages/leaderindex/leaderindex'
									});
								}

							} catch (error) {
								// 处理请求失败的逻辑
								console.error('请求失败', error);
							}
						}

					} catch (error) {
						// 处理请求失败的逻辑
						console.error('请求失败', error);
					}
				} else {
					that.$refs.toast.show({

						content: '点击了拒绝',
						icon: 'none', // 不显示图标
						duration: 2000 // 显示时长 2000ms
					});
				}


			}

		}
	}
</script>

<style lang="scss" scoped>
	
	.login_index {
		display: flex;
		flex-direction: column;
		height: 100vh;
		    background-image: url('https://srm.sfeg.cc/portal/apps/com.awspaas.user.apps.sfeg.srm.contractworkers/img/Shanghai.jpg');
		    /* 设置背景图的显示方式，可以根据需要调整 */
		    background-size: cover;
		    background-position: center;

		.login_index_box {
			margin-top: 186rpx;

			.login_index_logo {
				text-align: center;
			}

			.login_index_logo1 {
				text-align: center;
				margin-top: 35rpx;
				    font-size: 46rpx;
					 font-family: 'HanYiZongYi';
					     font-weight: 700;
			}
		}

		.login_index_btn {
			margin-top: auto; // 将其推到底部
			text-align: center;
			margin-bottom: 46rpx;

			button {
				// padding: 21rpx 197rpx;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 40rpx;
				color: #FFFFFF;
				background: linear-gradient(90deg, #3A84FD 0%, #536AFF 100%);
				border-radius: 40rpx;
				margin: 0 78rpx;
			}
		}

		.login_index_agree {
			font-family: Microsoft YaHei;
			font-weight: 400;
			font-size: 28rpx;
			display: flex;
			justify-content: center;
			text-align: center;
			color: #333333;
			margin-bottom: 97rpx;
		}
	}
</style>